package com.bootdo.bj.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.bootdo.common.controller.BaseController;
import com.bootdo.bj.domain.Category;
import com.bootdo.bj.domain.Context;
import com.bootdo.bj.service.CategoryService;
import com.bootdo.bj.service.ContextService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author haojiangbo
 * @email 2100272728@qq.com
 * @date 2019-01-23 17:27:07
 */
 
@Controller
@RequestMapping("/bj/context")
public class ContextController extends BaseController{

	@Autowired
	private CategoryService categoryService;
	@Autowired
	private ContextService contextService;
	
	@ModelAttribute
	public Context get(@RequestParam(required=false) Long id) {
		if (id != null){
			return contextService.get(id);
		}else{
			return new Context();
		}
	}


	
	//insert into sys_menu(parent_id,name,url,perms,type)  values (149,'测试','/bj/context','bj:context:context',1)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (160,'修改','','bj:context:edit',2)
	//type 0是分类菜单 1是小菜但 2是按钮
	@GetMapping()
	@RequiresPermissions("bj:context:context")	
	String Context(Context context,HttpServletRequest request,HttpServletResponse response){
	    return "bj/context/context";
	}
	
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("bj:context:context")
	public PageUtils list(@RequestParam Map<String, Object> params){	
		params.put("userId", getUserId());	
		//查询列表数据
        Query query = new Query(params);
		List<Context> contextList = contextService.list(query);
		int total = contextService.count(query);
		PageUtils pageUtils = new PageUtils(contextList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("bj:context:edit")
	String add(Context context,HttpServletRequest request,HttpServletResponse response){
	    return "bj/context/contextedit";
	}
	
	
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("bj:context:edit")
	public R save( Context context){
		if(context.getBjCategoryId() != null) {
			Category category = categoryService.get(context.getBjCategoryId());
			if(null != category) {
				context.setBjCategoryName(category.getName());
			}
		}
		context.setUserId(getUserId());
		context.setUserName(getUsername());
		if(contextService.save(context)>0){
			return R.ok();
		}
		return R.error();
	}
	
	
	/**
	 * 得到页面
	 * @throws IOException 
	 */
	@RequestMapping("/getHtml")
	@RequiresPermissions("bj:context:edit")
	public String getHtml( Context context,HttpServletRequest request,HttpServletResponse response) throws IOException{
	 
		return "bj/context/contextV";
	}
	
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("bj:context:edit")
	public R remove( Long id){
		if(contextService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("bj:context:edit")
	public R remove(@RequestParam("ids[]") Long[] ids){
		contextService.batchRemove(ids);
		return R.ok();
	}
	
}